/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hijor.mail;

import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 *
 * @author lwang
 */
public class SimpleMailSender {

    private final static int ContextType_Text = 1;
    private final static int ContextType_Html = 2;

    /**
     * 以文本格式发送邮件
     *
     * @param mailInfo 待发送的邮件的信息
     */
    public boolean sendTextMail(MailSenderInfo mailInfo) {
        return sendMailByTextType(mailInfo, ContextType_Text);
    }

    /**
     * 以HTML格式发送邮件
     *
     * @param mailInfo 待发送的邮件信息
     */
    public static boolean sendHtmlMail(MailSenderInfo mailInfo) {
        return sendMailByTextType(mailInfo, ContextType_Html);
    }

    private static boolean sendMailByTextType(MailSenderInfo mailInfo, int contentType) {
        // 判断是否需要身份认证   
        MyAuthenticator authenticator = null;
        Properties pro = mailInfo.getProperties();
        //如果需要身份认证，则创建一个密码验证器    
        if (mailInfo.isValidate()) {
            authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());
        }
        // 根据邮件会话属性和密码验证器构造一个发送邮件的session   
        Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
        try {
            // 根据session创建一个邮件消息   
            Message mailMessage = new MimeMessage(sendMailSession);
            // 创建邮件发送者地址   
            Address from = new InternetAddress(mailInfo.getFromAddress());
            // 设置邮件消息的发送者
            mailMessage.setFrom(from);
            // 创建邮件的接收者地址，并设置到邮件消息中   
            InternetAddress[] toAddrs = InternetAddress.parse(mailInfo.getToAddress(), false);
//            Address to = new InternetAddress(mailInfo.getToAddress());
            // Message.RecipientType.TO属性表示接收者的类型为TO   
            mailMessage.setRecipients(Message.RecipientType.TO, toAddrs);
            if (mailInfo.getCC() != null) {
                InternetAddress[] ccAddrs = InternetAddress.parse(mailInfo.getCC(), false);
                mailMessage.setRecipients(Message.RecipientType.CC, ccAddrs);
            }
            if (mailInfo.getBcc() != null) {
                InternetAddress[] bccAddrs = InternetAddress.parse(mailInfo.getBcc(), false);
                mailMessage.setRecipients(Message.RecipientType.BCC, bccAddrs);
            }
            // 设置邮件消息的主题   
            mailMessage.setSubject(mailInfo.getSubject());
            // 设置邮件消息发送的时间   
            mailMessage.setSentDate(new Date());
            // MiniMultipart类是一个容器类，包含MimeBodyPart类型的对象   
            // 将MiniMultipart对象设置为邮件内容   

            if (ContextType_Text == contentType) {
                String mailContent = mailInfo.getContent();
                mailMessage.setText(mailContent);
            } else {
                Multipart mainPart = new MimeMultipart();
                // 创建一个包含HTML内容的MimeBodyPart   
                BodyPart html = new MimeBodyPart();
                // 设置HTML内容   
                html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");
                mainPart.addBodyPart(html);
                mailMessage.setContent(mainPart);
            }
            // 发送邮件   
            Transport.send(mailMessage);
            return true;
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        //一次性总发送50封
        MailSenderInfo mailInfo = new MailSenderInfo();
        mailInfo.setMailServerHost("smtp.163.com");
//         mailInfo.setMailServerHost("smtp.qq.com");
        mailInfo.setMailServerPort("25");
        mailInfo.setValidate(true);
          mailInfo.setFromAddress("cx_coder@163.com");
        mailInfo.setUserName("cx_coder@163.com");
//         mailInfo.setFromAddress("769195688@qq.com");
//         mailInfo.setUserName("769195688@qq.com");
        mailInfo.setPassword("x123123.");//您的邮箱密码   
        StringBuilder toaddrs=new StringBuilder("769195688@qq.com");
//        for (int i = 0; i < 49; i++) {
//            toaddrs.append(",769195688@qq.com");
//        }
        mailInfo.setToAddress(toaddrs.toString());
//        StringBuilder ccaddrs=new StringBuilder("cx_coder@163.com");
//        for (int i = 0; i < 39; i++) {
//            ccaddrs.append(",769195688@qq.com");
//        }
//        mailInfo.setCC(ccaddrs.toString());
//        mailInfo.setBcc("i.coding.any@gmail.com");
        mailInfo.setSubject("hijor 秘密抄送加抄送测试max num");
        mailInfo.setContent("你好123123");
        //这个类主要来发送邮件  
        SimpleMailSender sms = new SimpleMailSender();
        sms.sendTextMail(mailInfo);//发送文体格式   
//        sms.sendHtmlMail(mailInfo);//发送html格式  
    }
}
